/**
 * @author lei yu
 * @since 2025/04/28
 */

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import com.example.traning.mall.framework.mybatis.core.BaseDO;
import com.example.traning.mall.framework.mybatis.core.mapper.BaseMapperX;
import org.apache.ibatis.annotations.Mapper;

import static com.baomidou.mybatisplus.generator.config.TemplateType.XML;

/**
 * @author lei yu
 * @since 2024/09/25
 */
public class AutoGeneratorUtils {

    private static final String URL = "jdbc:mysql://localhost:3306/tedu_tea_mall?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8";
    private static final String USERNAME = "admin";
    private static final String PASSWORD = "admin.me21";
    private static final String AUTHOR = "Lei Yu";
    private static final String OUT = System.getProperty("user.dir") + "/src/main";

    private static final String OUT_JAVA = OUT + "/java";
    private static final String OUT_XML = OUT + "/resources/mapper";
    private static final String PARENT = "com.example.traning.mall";

    public static void main(String[] args) {
        System.out.println("OUT_JAVA = " + OUT_JAVA);
        FileUtil.mkParentDirs(OUT_JAVA);

        AutoGeneratorUtils.gen_account();
        AutoGeneratorUtils.gen_content();
        AutoGeneratorUtils.gen_dict();
        AutoGeneratorUtils.gen_mall();
    }

    public static void gen_account() {
        PackageConfig packageConfig = new PackageConfig.Builder()
                .parent(PARENT)
                .entity("dal.dataobject.account")
                .mapper("dal.mysql.account")
                .service("service.account")
                .serviceImpl("service.account.impl")
                .controller("controller.account")
                .build();
        String[] tables = {"account_login_log", "account_permission", "account_role", "account_role_permission", "account_user", "account_user_role"};
        AutoGeneratorUtils.gen(packageConfig, tables);
    }

    public static void gen_content() {
        PackageConfig packageConfig = new PackageConfig.Builder()
                .parent(PARENT)
                .entity("dal.dataobject.content")
                .mapper("dal.mysql.content")
                .service("service.content")
                .serviceImpl("service.content.impl")
                .controller("controller.content")
                .build();
        String[] tables = {"content_article", "content_article_detail", "content_category", "content_check_log", "content_comment", "content_up_down_log"};
        AutoGeneratorUtils.gen(packageConfig, tables);
    }

    public static void gen_dict() {
        PackageConfig packageConfig = new PackageConfig.Builder()
                .parent(PARENT)
                .entity("dal.dataobject.dict")
                .mapper("dal.mysql.dict")
                .service("service.dict")
                .serviceImpl("service.dict.impl")
                .controller("controller.dict")
                .build();
        AutoGeneratorUtils.gen(packageConfig, "dict_district");
    }

    public static void gen_mall() {
        PackageConfig packageConfig = new PackageConfig.Builder()
                .parent(PARENT)
                .entity("dal.dataobject.mall")
                .mapper("dal.mysql.mall")
                .service("service.mall")
                .serviceImpl("service.mall.impl")
                .controller("controller.mall")
                .build();
        String[] tables = {"mall_category", "mall_check_log", "mall_comment", "mall_goods", "mall_goods_detail", "mall_order", "mall_order_item", "mall_receiver_address"};
        AutoGeneratorUtils.gen(packageConfig, tables);
    }

    public static void gen(PackageConfig packageConfig, String... include) {
        new AutoGenerator(new DataSourceConfig.Builder(URL, USERNAME, PASSWORD).build())
                // 全局配置
                .global(new GlobalConfig.Builder()
                        // 作者
                        .author(AUTHOR)
                        // 时间类型：TIME_PACK => LocalDateTime
                        .dateType(DateType.TIME_PACK)
                        // 输出：./src/main/java
                        .outputDir(OUT_JAVA).build()
                )
                // 包信息
                .packageInfo(packageConfig)
                // 策略
                .strategy(new StrategyConfig.Builder()
                                // 包含哪些表，默认是所有
                                .addInclude(include)
                                // Entity 策略配置
                                .entityBuilder()
                                .superClass(BaseDO.class)
                                .disableSerialVersionUID()
                                .enableChainModel()
                                .enableLombok()
                                .enableRemoveIsPrefix()
//                                .enableTableFieldAnnotation()
//                                .enableActiveRecord()
//                                .versionColumnName("version")
//                                .versionPropertyName("version")
//                                .logicDeleteColumnName("deleted")
//                                .logicDeletePropertyName("deleteFlag")
                                .naming(NamingStrategy.underline_to_camel)
                                .columnNaming(NamingStrategy.underline_to_camel)
//                                .addIgnoreColumns("age")
                                .addTableFills(new Column("gmt_create", FieldFill.INSERT))
                                .addTableFills(new Property("gmt_modified", FieldFill.INSERT_UPDATE))
                                .idType(IdType.AUTO)
                                .formatFileName("%s")
                                // Controller 策略配置
                                .controllerBuilder()
                                // 使用 @RestController
                                .enableRestStyle()
                                // Mapper 策略配置
                                .mapperBuilder()
                                .mapperAnnotation(Mapper.class)
                                .superClass(BaseMapperX.class)
                                .build()
                )

                // 不生成 xml文件
                .template(new TemplateConfig.Builder().disable(XML/*, SERVICE, SERVICE_IMPL, CONTROLLER*/)
                        .service("/templates/service2.java")
                        .serviceImpl("/templates/serviceImpl2.java")
                        .controller("/templates/controller2.java")
                        .build())
                .execute();
    }
}
